package com.university.report.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.university.report.model.entity.Permission;
import com.university.report.model.entity.UserAccount;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName health_report
 * @Author Jinondo
 * @Date 2021/6/16 17:37
 */
@Repository
public interface UserAccountMapper extends BaseMapper<UserAccount> {
    /**
     * 根据用户名称查询
     *
     * @param userName
     * @return
     */
    UserAccount selectByUsername(@Param("userName") String userName);

    /**
     * 查询用户的权限根据用户查询权限
     *
     * @param username
     * @return
     */
    @Select(" select permission.* from t_user user" + " inner join t_user_role user_role"
            + " on user.user_id = user_role.user_id inner join "
            + "t_role_permission role_permission on user_role.role_id = role_permission.role_id "
            + " inner join t_permission permission on role_permission.perm_id = permission.perm_id where user.username = #{username};")
    List<Permission> findPermissionByUsername(@Param("username") String username);

    @Insert("insert into t_user_role (user_id,role_id,create_time, update_time) values(#{userId},#{roleId},now(),now())")
    Integer insertOne(Integer userId, Integer roleId);

    @Select("select t_role.role_name from t_role,t_user_role where t_user_role.user_id=#{userId} and t_user_role.role_id=t_role.role_id")
    String findRoleNameByUserId(@Param("userId")Integer userId);
}
